package com.one.efaimaly.order.ui;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.callback.OnBinderItemClickListener;
import com.one.common.common.order.MaterialItem;
import com.one.common.config.RouterPath;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.StringUtils;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.efaimaly.R;
import com.one.efaimaly.order.presenter.MaterialPresenter;
import com.one.efaimaly.order.ui.binder.SelectMaterialBinder;

/**
 * creator: zhulunjun
 * time:    2018/8/9
 * describe:
 */
@Route(path = RouterPath.SELECT_MATERIAL)
public class SelectMaterialActivity extends BaseListActivity<MaterialPresenter> implements IListView,
        OnBinderItemClickListener<MaterialItem> ,
        TextWatcher{
    @Override
    protected void initPresenter() {
        mPresenter = new MaterialPresenter(this, this);
    }

    @Override
    public void registerDate() {
        register(MaterialItem.class, new SelectMaterialBinder(this));
    }

    @Override
    public void loadData() {
        mPresenter.getMaterial();
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("选择材料");
    }

    @Override
    public void initView() {
        super.initView();
        addTopView(R.layout.layout_select_city_top);
        final EditText etSearch = successView.findViewById(R.id.et_search);
        etSearch.setHint("请输入材料名称");
        TextView btnSearch = successView.findViewById(R.id.tv_search);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.getMaterial(etSearch.getText().toString());
            }
        });

        etSearch.addTextChangedListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getMaterial();
    }

    @Override
    public void onItemClick(View v, int position, MaterialItem o) {
        Intent intent =new Intent();
        // 类型
        DefaultExtra typeExtra = (DefaultExtra) getIntent().getSerializableExtra(DefaultExtra.getExtraName());
        int type = typeExtra.getNum();
        // 类型和名称
        DefaultExtra defaultExtra = new DefaultExtra(o.getName(), type);
        intent.putExtra(DefaultExtra.getExtraName(), defaultExtra);
        setResult(RESULT_OK, intent);
        finishPage();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(StringUtils.isBlank(charSequence.toString())){
            mPresenter.getMaterial();
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
